<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div id="app">
        用户名：<input type="text" v-model.trim.lazy='uname'>
        <span>{{tip}}</span>
    </div>
    <script src="js/vue.js"></script>
    <script>
        const vue = new Vue({
            el: '#app',
            data: {
                uname: '',
                tip: ''
            },
            watch: {
                uname: function(val) {
                    console.log('uname Change: ' + val);
                    const that = this;
                    // 调用后台接口验证用户名合法性
                    setTimeout(() => {
                        if (val === 'admin') {
                            that.tip = '用户名已存在';
                        } else {
                            that.tip = '用户名可以使用';
                        }
                    }, 2000);
                    this.tip = '正在验证...';
                }
            }
        });
    </script>
</body>

</html>